{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "该Jupyter Notebook用于展示如何使用ScriptTraderApp模块，在CLI命令行下进行交易指令的调用"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "from vnpy.app.script_trader import init_cli_trading\r\n",
    "from vnpy_ctp import CtpGateway\r\n",
    "from vnpy.trader.utility import load_json"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 连接到服务器\r\n",
    "setting = load_json(\"connect_ctp.json\")\r\n",
    "engine = init_cli_trading([CtpGateway])\r\n",
    "engine.connect_gateway(setting, \"CTP\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询所有合约\r\n",
    "engine.get_all_contracts(use_df=True)"
   ],
   "outputs": [],
   "metadata": {
    "scrolled": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询资金\n",
    "engine.get_all_accounts(use_df=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询持仓\n",
    "engine.get_all_positions(use_df=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询活动委托\n",
    "engine.get_all_active_orders(use_df=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 订阅行情\n",
    "engine.subscribe([\"600036.SSE\"])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询行情\n",
    "engine.get_tick(\"600036.SSE\", use_df=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 委托下单\n",
    "vt_orderid = engine.buy(\"600036.SSE\", 32, 1000)\n",
    "print(vt_orderid)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 查询特定委托\n",
    "engine.get_order(vt_orderid)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 委托撤单\n",
    "engine.cancel_order(vt_orderid)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}